package com.ximu.car.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ximu.car.entity.Car;
import com.ximu.car.mapper.CarMapper;
import com.ximu.common_utlis.jwt.JwtUtil;
import com.ximu.common_utlis.result.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ximu
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/car")
@Api(tags = "我的车辆接口")
public class CarController {

    @Autowired
    private CarMapper carMapper;

    @ApiOperation(value = "查询用户车辆")
    @PostMapping("findAllCars")
    public Result findAllCars(@RequestHeader("Authorization") String token){

        try {
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();

            QueryWrapper<Car> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", Integer.parseInt(subject));

            List<Car> cars = carMapper.selectList(queryWrapper);
            return Result.ok().data("cars",cars).message("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("查询失败，检查是否登录");
    }

    @ApiOperation(value = "新增用户车辆")
    @PostMapping("addCars")
    public Result findAllCars(@RequestHeader("Authorization") String token, @RequestBody Car car){

        try {
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();

            car.setUserId(Integer.valueOf(subject));
            car.setCarId(null);
            int rs = carMapper.insert(car);
            if (rs > 0) {
                QueryWrapper<Car> queryWrapper = new QueryWrapper();
                queryWrapper.eq("user_id", Integer.parseInt(subject));

                List<Car> cars = carMapper.selectList(queryWrapper);
                return Result.ok().data("cars",cars).message("新增成功，新的列表如下");
            } else {
                return Result.error().message("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("新增失败，检查是否登录");
    }

    @ApiOperation(value = "删除用户车辆")
    @PostMapping("deleteCars")
    public Result deleteCars(@RequestHeader("Authorization") String token, @RequestBody Car car){

        try {
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();

            //判断是否有车
            QueryWrapper<Car> queryWrapper = new QueryWrapper();
            queryWrapper.lambda().eq(Car::getCarId, car.getCarId());
            queryWrapper.lambda().eq(Car::getUserId, subject);
            //删除
            int rs = carMapper.delete(queryWrapper);
            if (rs > 0) {
                QueryWrapper<Car> queryWrapper2 = new QueryWrapper();
                queryWrapper.eq("user_id", Integer.parseInt(subject));

                List<Car> cars = carMapper.selectList(queryWrapper2);
                return Result.ok().data("cars",cars).message("删除成功，新的列表如下");
            } else {
                return Result.error().message("删除失败，别乱交post了，删不了别人车的");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("删除失败，检查是否登录");
    }


}

